package br.com.erpcortume.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import br.com.erpcortume.beans.Cliente;
import br.com.erpcortume.services.ClienteServices;
import br.com.erpcortume.utils.Tag;

@Controller
public class MainController {

	
	@Autowired
	private ClienteServices clienteServices;
	
	
	@RequestMapping(value = "/getTags", method = RequestMethod.GET)
	public @ResponseBody
	List<Tag> getTags(@RequestParam String tagName) {
		return searchResult(tagName);
	}
	

	private List<Tag> searchResult(String tagName) {
		
		List<Tag> data = new ArrayList<Tag>();
		
		List<Cliente> clienteByName = clienteServices.findClienteByName(tagName);
		
		for (Cliente c : clienteByName) {
			
			data.add(new Tag(c.getId(), c.getNome()));
			
		}
		
		List<Tag> result = new ArrayList<Tag>();

		// iterate a list and filter by tagName
		for (Tag tag : data) {
			if (tag.getTagName().contains(tagName)) {
				result.add(tag);
			}
		}

		return result;
	}
	
	
}
